// pages/order/order.js
const app = getApp()
var util = require("../../utils/utils.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    successOrderList:[],
    pendingOrderList:[],
    allOrderList:[],
    currentData: 0,
    successPage: 1,
    pendingPage: 1,
    allPage: 1,
    successSearchLoading: false, 
    successSearchLoadingComplete: false,
    pendingSearchLoading: false,
    pendingSearchLoadingComplete: false,
    allSearchLoading: false,
    allSearchLoadingComplete: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.searchScroll();
  },
  //获取当前滑块的index
  bindchange: function (e) {
    const that = this;
    that.setData({
      currentData: e.detail.current
    })
  },
  //点击切换，滑块index赋值
  checkCurrent: function (e) {
    const that = this;

    if (that.data.currentData === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentData: e.target.dataset.current
      })
      if (that.data.currentData == 2 && that.data.successPage == 1) {
        this.searchScroll()
      }
      else if (that.data.currentData == 1 && that.data.pendingPage == 1) {
        this.searchScroll()
      }
      else if (that.data.currentData == 0 && that.data.allPage == 1) {
        this.searchScroll()
      }
      
    }
  },
  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  // },
  fitchSearch: function() {
    let that = this;
    var params = {};
    if (that.data.currentData == 2) {
      params.page = that.data.successPage;
      params.orderStatus = '000';
    }
    else if (that.data.currentData == 1) {
      params.page = that.data.pendingPage;
      params.orderStatus = '001';
    }
    else if (that.data.currentData == 0) {
      params.page = that.data.allPage;
      params.orderStatus = '';
    }
    params.rows = 8;
    // 分页查询我的订单列表
    wx.request({
      url: app.httpUrl + '/user/queryOrderList',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      data: params,
      method: "POST",
      success: (res) => {
        if (that.data.currentData == 0) {
          that.setData({
            allPage: that.data.allPage += 1,
            successSearchLoading: false,
            successSearchLoadingComplete: false
          })
        }
        else if (that.data.currentData == 1) {
          that.setData({
            pendingPage: that.data.pendingPage += 1,
            pendingSearchLoading: false,
            pendingSearchLoadingComplete: false
          })
        }
        else if (that.data.currentData == 2) {
          that.setData({
            successPage: that.data.successPage += 1,
            allSearchLoading: false,
            allSearchLoadingComplete: false
          })
        }
        var list = res.data.data.results;
        console.log(list)
        if(!list.length){
          that.setData({
            allSearchLoading: false,
            allSearchLoadingComplete: true
          })
        }
        for (var i in list) {
          list[i].orderCreateTime = util.formatDate(list[i].orderCreateTime, 'Y年M月D日');
          if (that.data.currentData == 0) {
            that.data.allOrderList.push(list[i])
          }
          else if (that.data.currentData == 1) {
            that.data.pendingOrderList.push(list[i])
          }
          else if (that.data.currentData == 2) {
            that.data.successOrderList.push(list[i])
          }
        }
        if (that.data.currentData == 0) {
          that.setData({
            allOrderList: that.data.allOrderList
          })
        }
        else if (that.data.currentData == 1) {
          that.setData({
            pendingOrderList: that.data.pendingOrderList
          })
        }
        else if (that.data.currentData == 2) {
          that.setData({
            successOrderList: that.data.successOrderList
          })
        }
        if (that.data.currentData == 0) {
          if (that.data.allPage >= res.data.data.totalPage) {
            that.setData({
               allSearchLoadingComplete: true,
              allSearchLoading: false
            });
          }
        }
        else if (that.data.currentData == 1) {
          if (that.data.pendingPage >= res.data.data.totalPage) {
            that.setData({
              pendingSearchLoadingComplete: true,
              pendingSearchLoading: false
            });
          }
        }
        else if (that.data.currentData == 2) {
          if (that.data.successPage >= res.data.data.totalPage) {
            that.setData({
              successSearchLoadingComplete: true,
              successSearchLoading: false
            });
          }
        }
      }
    })
  },
  /**
   * 跳转页面
   */
  jump:function(e){
    let url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    })
  },
  /**
   * gopay
   */
  jumps:function(e){
    let url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url:url,
    })
  },
  searchScroll: function () {
    let that = this;
    if (that.data.currentData == 2){
      if (!that.data.successSearchLoading && !that.data.successSearchLoadingComplete) {
        that.setData({
          successSearchLoading: true,
          successSearchLoadingComplete: true
        })
        that.fitchSearch();
      }
    }
    else if (that.data.currentData == 1) {
      if (!that.data.pendingSearchLoading && !that.data.pendingSearchLoadingComplete) {
        that.setData({
          pendingSearchLoading: true,
          pendingSearchLoadingComplete: true
        })
        that.fitchSearch();
      }
    }
    else if (that.data.currentData == 0) {
      if (!that.data.allSearchLoading && !that.data.allSearchLoadingComplete) {
        that.setData({
          allSearchLoading: true,
          allSearchLoadingComplete: true
        })
        that.fitchSearch();
      }
    }
  }
})